package repository

import entity.Role
import entity.Udr
import org.hibernate.criterion.Restrictions

class UdrRepositoryImpl : UdrRepository {
    override fun findByUserId(userId: Int): Set<Udr>? {
        // @Query("select u from auth_r_user_group_role u where user_id=?1")
        return executeTransaction { session ->
            val hql = "FROM Udr U WHERE U.user.id = :userId"
            val query = session.createQuery(hql)
            query.setParameter("userId", userId)
            query.list().map { it as Udr }.toSet()
        }
    }

    override fun findByRole(role: Role): Set<Udr>? {
        return executeTransaction { session ->
            val cr = session.createCriteria(Udr::class.java)
            cr.add(Restrictions.eq("role", role))
            cr.list().map { it as Udr }.toSet()
        }
    }
}